package com.crm.system.controller.cluePoolManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.cluePoolManager.CluePoolPrivate;
import com.crm.model.entity.cluePoolManager.CluePoolPublic;
import com.crm.model.qo.cluePoolManager.CluePoolPrivateQO;
import com.crm.model.vo.cluePoolManager.CluePoolPrivateVO;
import com.crm.service.cluePoolManager.CluePoolPrivateDbService;
import com.crm.system.service.cluePoolManager.CluePoolPrivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wll
 * @date 2020/12/28 11:22
 */
@RestController
@RequestMapping("/cluePoolPrivate")
public class CluePoolPrivateController {
    @Autowired
    CluePoolPrivateService cluePoolPrivateService;

    @Autowired
    private CluePoolPrivateDbService cluePoolPrivateDbService;

    /**
     * @author wll
     * @date 2020/12/29 10:00
     * info 用户模块-我的线索池
     * @param pageIndex
     * @param pageSize
     * @param cluePoolPrivateInfo  搜索条件
     * @param sort 排序条件
     * @param sortOrder 正序/倒序
     * @return
     */
    @PostMapping("/pageList")
    public DataResponse getList(@RequestParam(value = "pageIndex") Integer pageIndex,
                                @RequestParam(value = "pageSize") Integer pageSize,
                                @RequestParam(value = "sort", required = false) String sort,
                                @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                CluePoolPrivateQO cluePoolPrivateInfo) {
        DhPageInfo<CluePoolPrivateVO> cluePoolPrivates = cluePoolPrivateService.getList(pageIndex, pageSize, cluePoolPrivateInfo,sort,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), cluePoolPrivates);
    }



    /**
     * @date 2020/12/28 11:30
     * @author wll
     * info 用户模块-创建线索池
     * @param cluePoolPrivateQO
     * @return
     */
    @PostMapping("/save")
    public DataResponse add(@RequestBody CluePoolPrivateQO cluePoolPrivateQO) {
        //根据线索池名称判断是否重复 null兼容
        CluePoolPrivate cluePoolPrivateInfo = cluePoolPrivateDbService.selectCluePoolPrivateByName(cluePoolPrivateQO.getCluePoolName(),null);
        if(null == cluePoolPrivateInfo){
            Boolean result = cluePoolPrivateService.add(cluePoolPrivateQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
            }
        }else{
            return new DhResponse(ResponseCodeEnum.POOL_NAME_DUPLICATE.getResponseCode());
        }

    }



    /**
     * @author wll
     * @date 2021/02/01 11:30
     * info 用户模块-我的线索池-修改
     * @param cluePoolPrivateQO
     * @return
     */
    @PostMapping("/modify")
    public DataResponse update(@RequestBody CluePoolPrivateQO cluePoolPrivateQO) {
        //根据线索池名称判断是否重复 不等于当前线索池id的
        CluePoolPrivate cluePoolPrivateInfo = cluePoolPrivateDbService.selectCluePoolPrivateByName(cluePoolPrivateQO.getCluePoolName(),cluePoolPrivateQO.getCluePoolPrivateId());
       if(null == cluePoolPrivateInfo){
            Boolean result = cluePoolPrivateService.update(cluePoolPrivateQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
            }
        }else{//线索池名称已经重复
            return new DhResponse(ResponseCodeEnum.POOL_NAME_DUPLICATE.getResponseCode());
        }

    }




    /**
     * @author wll
     * @date 2021/02/01
     * info 用户模块-我的线索池-删除
     * @param id 线索池id
     * @return
     */
    @GetMapping ("/delete")
    public DataResponse delete(String id) {
            Boolean result = cluePoolPrivateService.delete(id);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
            }


    }
}
